﻿
using Sang.IoT.SSD1306;
using SkiaSharp;
using System.Collections.Generic;
using System.Drawing;
namespace IotDeviceToolHepler.Device
{
    /// <summary>
    /// SSD1306显示屏 
    /// </summary>
    public class DeviceDisplay_SSD1306
    {
        //https://github.com/sangyuxiaowu/Sang.IoT.SSD1306
        //https://blog.csdn.net/weixin_43288375/article/details/131148818
        /// <summary>
        /// https://cloud.tencent.com/developer/article/2167580?areaSource=102001.7&traceId=4btjeE-8zxMvzfTF_T0VN
        /// </summary>
        public void Test2()
        {
            DisplayTest();
        }

        public void DisplayTest()
        {
            Console.WriteLine("Start. Press Ctrl+C to end.");
            // 显示准备
            using var oled = new SSD1306_128_64(0);
            oled.Begin();
            oled.Clear();

            var bitmap = new SKBitmap(128, 64, true);
            SKPaint paint = new SKPaint()
            {
                Color = new SKColor(255, 255, 255),
                StrokeWidth = 1, //画笔宽度
                Typeface = SKTypeface.FromFile("/usr/share/fonts/truetype/wqy/MSYH.TTC"),
                TextSize = 13,  //字体大小
                Style = SKPaintStyle.Fill,
            };
            SKCanvas canvas = new SKCanvas(bitmap);
            while (true)
            {
                paint.TextSize = 13;
                canvas.DrawText(System.DateTime.Now.ToString("yyy-MM-dd HH:mm:ss"), 0, 12, paint);
                paint.TextSize = 30;
                canvas.DrawText("设备正常", 0, 50, paint);
                oled.Image(bitmap.Encode(SKEncodedImageFormat.Png, 100).ToArray());
                oled.Display();
                canvas.Clear();
                Thread.Sleep(1000);
            }
        }


        public void Test()
        {
            while (true)
            {
                List<DeviceDisplay_SSD1306Model> models = new List<DeviceDisplay_SSD1306Model>();
                DeviceDisplay_SSD1306Model deviceDisplay = new DeviceDisplay_SSD1306Model();
                deviceDisplay.size = 13;
                deviceDisplay.content = System.DateTime.Now.ToString("yyy-MM-dd HH:mm:ss");
                deviceDisplay.x = 0;
                deviceDisplay.y = 13;
                models.Add(deviceDisplay);

                deviceDisplay = new DeviceDisplay_SSD1306Model();
                deviceDisplay.size = 30;
                deviceDisplay.content = "设备正常";
                deviceDisplay.x = 0;
                deviceDisplay.y = 50;
                models.Add(deviceDisplay);

                Display(0, models);
                Thread.Sleep(10000);
            }
        }



        static SSD1306_128_64 oled = null;
        static SKPaint paint = null;
        static SKCanvas canvas = null;
        static SKBitmap bitmap = null;
        /// <summary>
        /// 显示内容，供外部调用
        /// </summary>
        /// <param name="bus"></param>
        /// <param name="models"></param>
        public void Display(int bus, List<DeviceDisplay_SSD1306Model> models)
        {
            if (oled == null)
            {
                // 显示准备
                oled = new SSD1306_128_64(bus);
                oled.Begin();
                oled.Clear();

                bitmap = new SKBitmap(128, 64, true);
                paint = new SKPaint()
                {
                    Color = new SKColor(255, 255, 255),
                    StrokeWidth = 1, //画笔宽度
                    Typeface = SKTypeface.FromFile("/usr/share/fonts/truetype/wqy/MSYH.TTC"),
                    TextSize = 13,  //字体大小
                    Style = SKPaintStyle.Fill,
                };
                canvas = new SKCanvas(bitmap);
            }


            foreach (DeviceDisplay_SSD1306Model item in models)
            {
                paint.TextSize = item.size;
                canvas.DrawText(item.content, item.x, item.y, paint);
            }
            oled.Image(bitmap.Encode(SKEncodedImageFormat.Png, 100).ToArray());
            oled.Display();
            canvas.Clear();
        }
        public class DeviceDisplay_SSD1306Model
        {
            public int size { get; set; }
            public string content { get; set; }
            public int x { get; set; }
            public int y { get; set; }
        }
    }
}
